package hunglv.android.rss.handler;

import hunglv.android.rss.entities.RSSFeed;
import hunglv.android.rss.entities.RSSItem;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RSSHandler extends DefaultHandler {
	private RSSFeed mRSSFeed;
	private RSSItem mRSSItem;

	String _lastElementName = "";
	boolean bFoundChannel = false;

	private static final int RSS_TITLE = 1;
	private static final int RSS_LINK = 2;
	private static final int RSS_DESCRIPTION = 3;
	private static final int RSS_CATEGORY = 4;
	private static final int RSS_PUBDATE = 5;

	int depth = 0;
	int currentstate = 0;

	public RSSFeed getFeed() {
		return mRSSFeed;
	}

	public void setFeed(RSSFeed _feed) {
		this.mRSSFeed = _feed;
	}

	public RSSItem getItem() {
		return mRSSItem;
	}

	public void setItem(RSSItem _item) {
		this.mRSSItem = _item;
	}

	// ----------------------------------------------------
	@Override
	public void startDocument() throws SAXException {
		mRSSFeed = new RSSFeed();
		mRSSItem = new RSSItem();
	}

	@Override
	public void endDocument() throws SAXException {

	}

	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		depth++;
		
		if (localName.equals("channel")) {
			currentstate = 0;
			return;
		}
		if (localName.equals("image")) {
			mRSSItem = new RSSItem();
			return;
		}
		if (localName.equals("item")) {
			// create a new item
			mRSSItem = new RSSItem();
			return;
		}
		if (localName.equals("title")) {
			currentstate = RSS_TITLE;
			return;
		}
		if (localName.equals("description")) {
			currentstate = RSS_DESCRIPTION;
			return;
		}
		if (localName.equals("link")) {
			currentstate = RSS_LINK;
			return;
		}
		if (localName.equals("category")) {
			currentstate = RSS_CATEGORY;
			return;
		}
		if (localName.equals("pubDate")) {
			currentstate = RSS_PUBDATE;
			return;
		}

		// if you don't explicitly handle the element, make sure you don't wind
		// up erroneously storing a newline or other bogus data into one of our
		// existing elements
		currentstate = 0;
	}
	
	@Override
	public void endElement(String nameSpaceUri, String localName, String qname) throws SAXException{
		depth --;
		if(localName.equals("item")){
			mRSSFeed.addItem(mRSSItem);
			return;
		}
	}
	
	@Override
	public void characters(char ch[], int start, int length){
		String  content = new String(ch, start, length);
		
		switch(this.currentstate){
			case RSS_CATEGORY:
				mRSSItem.setCategory(content);
				currentstate = 0;
				break;
			case RSS_DESCRIPTION:
				mRSSItem.setDescription(content);
				currentstate = 0;
				break;
			case RSS_LINK:
				mRSSItem.setLink(content);
				currentstate = 0;
				break;
			case RSS_PUBDATE:
				mRSSItem.setPubDate(content);
				currentstate = 0;
				break;
			case RSS_TITLE:
				mRSSItem.setTitle(content);
				currentstate = 0;
				break;
			default:
				break;
		}
	}
}
